//
//  GetPublicFeedsRequest.m
//  collection
//
//  Created by Jing on 4/24/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "GetPublicFeedsRequest.h"
#import "Feed.h"

@implementation GetPublicFeedsRequest

- (void)initParams {
    
    self.descriptionName = @"note.getpublictimeline";
    
    RequestData* requestData = [[GetPublicFeedsRequestData alloc] init];
    self.request = requestData;
    [requestData release];
    
    Response* response = [[GetPublicFeedsResponse alloc] init];
    self.response = response;
    [response release];
}

- (NSString*)buildRequestURL {
    
    GetPublicFeedsRequestData* requestData = (GetPublicFeedsRequestData*)self.request;
    
    NSString* requestContent = [NSString stringWithFormat:@"&uid=%lld&pageIndex=%d&sinceNoteId=%lld&type=%d",
                                requestData.UID,
                                requestData.page,
                                requestData.sinceFID,
                                requestData.type];
    
    return requestContent;
}

- (void)parseResponse:(NSObject *)data {
    
    GetPublicFeedsResponse* response = (GetPublicFeedsResponse*)self.response;
    
    NSDictionary* dic = (NSDictionary*)data;
    
    // Page index
    NSString* numStr = [dic objectForKey:@"pageIndex"];
    if (numStr != nil && ![numStr isEqualToString:@""]) {
        response.page = numStr.longLongValue;
    }
    
    // notes
    NSArray* rawNoteArray = [dic objectForKey:@"notes"];
    if (rawNoteArray != nil) {
        
        NSMutableArray* noteArray = [[NSMutableArray alloc] initWithCapacity:1];
        for (NSDictionary* userData in rawNoteArray) {
            Feed* feed = [self parseOneFeed:userData];
            if (feed != nil) {
                [noteArray addObject:feed];
                [feed release];
            }
        }
        
        response.feedList = noteArray;
        [noteArray release];
    }
}

@end
